package com.yueyang.sojourn.server.service.sojourn.order;

import com.alibaba.fastjson.JSON;
import com.yueyang.sojourn.server.model.enums.OrderEventTypeEnum;
import com.yueyang.sojourn.server.model.enums.OrderTypeEnum;
import com.yueyang.sojourn.server.model.po.SojournOrder;
import com.yueyang.sojourn.server.model.po.SojournOrderEvent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Andy.yang
 * @version v1
 * @summary
 * @Copyright (c) 2019, YueYang Group All Rights Reserved.
 * @since 2020/4/3
 */
@Slf4j
@Component
public class SojournOrderMsgListener {
    @Autowired
    SojournOrderMsgService msgService;
    @Autowired
    SojournOrderService orderService;
    //线程池
    ExecutorService executorService = Executors.newFixedThreadPool(3);

    @EventListener
    public void onApplicationEvent(SojournOrderEvent event) {
        log.debug("订单事件：" + JSON.toJSONString(event));
        SojournOrder order = orderService.findById(event.getOrderId());
        if(order == null || order.getType() == null || OrderTypeEnum.MERCH.getCode() != order.getType()){
            return;
        }
        //待付款：创建订单
        if(OrderEventTypeEnum.TO_BE_PAY.getCode() == event.getType()){
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    String travelTime = DateFormatUtils.format(order.getMerchEffectDate(),"yyyy-MM-dd");
                    msgService.sendMsgCreated(order.getCreatorPhone(),order.getSn(),order.getMerchTitle(),travelTime);
                }
            });
        }
        //待付款：即将过期
        else if(OrderEventTypeEnum.TO_BE_PAY_EXPRIE.getCode() == event.getType()){
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    log.debug("订单事件--即将过期开始：" + JSON.toJSONString(event));
                    String travelTime = DateFormatUtils.format(order.getMerchEffectDate(),"yyyy-MM-dd");
                    msgService.sendMsgWillExpire( order.getCreatorPhone(),order.getSn(),order.getMerchTitle(),travelTime);
                    log.debug("订单事件--即将过期结束：" + JSON.toJSONString(event));

                }
            });
        }
        //过期：关闭
        else if(OrderEventTypeEnum.CLOSED.getCode() == event.getType()){
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    String travelTime = DateFormatUtils.format(order.getMerchEffectDate(),"yyyy-MM-dd");
                    msgService.sendMsgExpired(order.getCreatorPhone(),order.getSn(),order.getMerchTitle(),travelTime);
                }
            });
        }
        //核销
        else if(OrderEventTypeEnum.DEALED.getCode() == event.getType()){
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    String travelTime = DateFormatUtils.format(order.getMerchEffectDate(),"yyyy-MM-dd");
                    msgService.sendMsgDealed(order.getCreatorPhone(),order.getSn(),order.getMerchTitle(),travelTime);
                }
            });
        }
        //退款
        else if(OrderEventTypeEnum.REFUNDED.getCode() == event.getType()){
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    String travelTime = DateFormatUtils.format(order.getMerchEffectDate(),"yyyy-MM-dd");
                    msgService.sendMsgRefunded(order.getCreatorPhone(),order.getSn(),order.getMerchTitle(),travelTime);
                }
            });
        }
    }
}
